package com.lry.jvm.classfile.constantpool;

import com.lry.jvm.classfile.ClassReader;

public class ConstantMemberRefInfo implements ConstantInfo {

    private ConstantPool constPool;

    private int classIndex;
    private int nameAndTypeIndex;

    public ConstantMemberRefInfo(ConstantPool aConstPool, ClassReader reader) {
        this.classIndex = reader.nextU2ToInt();
        this.nameAndTypeIndex = reader.nextU2ToInt();
        this.constPool = aConstPool;
    }

    public String getValue() {
        return constPool.getUTF8(classIndex)
                + " " + constPool.getUTF8(nameAndTypeIndex);
    }

    public String name(){
        ConstantInfo[] constantInfos = constPool.getConstantInfos();
        ConstantNameAndTypeInfo nameAndTypeInfo = (ConstantNameAndTypeInfo) constantInfos[nameAndTypeIndex];
        return constPool.getUTF8(nameAndTypeInfo.getNameIndex());
    }
    public String descriptor(){
        ConstantInfo[] constantInfos = constPool.getConstantInfos();
        ConstantNameAndTypeInfo nameAndTypeInfo = (ConstantNameAndTypeInfo) constantInfos[nameAndTypeIndex];
        return constPool.getUTF8(nameAndTypeInfo.getDescriptorIndex());
    }

    public String className(){
        return constPool.getUTF8(classIndex);
    }

    @Override
    public String toString() {
        ConstantInfo[] constantInfos = constPool.getConstantInfos();
        ConstantClassInfo constClassInfo = (ConstantClassInfo) constantInfos[classIndex];
        ConstantNameAndTypeInfo nameAndTypeInfo = (ConstantNameAndTypeInfo) constantInfos[nameAndTypeIndex];
        return "ConstantMemberrefInfo{" +
                constClassInfo + "  " +
                nameAndTypeInfo +
                '}';
    }

}